package com.estock.common.model.security.enums;

import org.apache.commons.lang.StringUtils;

public enum StockType {
	SH(1, "上证"), SZ(2, "深证");
	
	private int type;
	private String desc;
	
	StockType(int type, String desc) {
		this.type = type;
		this.desc = desc;
	}
	
	public int getType() {
		return this.type;
	}
	
	public String getDesc() {
		return desc;
	}

	public static String getAbbrByStockType(int type) {
		if (StockType.SH.type == type) {
			return StockType.SH.name();
		} else if (StockType.SZ.type == type) {
			return StockType.SZ.name();
		}
		throw new RuntimeException("Unsupported stock type.");
	}
	
	public static String getDescByStockType(int type) {
		if (StockType.SH.type == type) {
			return StockType.SH.desc;
		} else if (StockType.SZ.type == type) {
			return StockType.SZ.desc;
		}
		throw new RuntimeException("Unsupported stock type.");
	}
	
	public static int getStockTypeByAbbr(String abbr) {
		String lowerAbbr = StringUtils.upperCase(abbr);
		if (StringUtils.equals(StockType.SH.name(), lowerAbbr)) {
			return StockType.SH.type;
		} else if (StringUtils.equals(StockType.SZ.name(), lowerAbbr)) {
			return StockType.SZ.type;
		}
		throw new RuntimeException("Unsupported stock type.");
	}
	
	public static StockType getStockTypeByCode(String code) {
		if (code.startsWith("6")) {
			return StockType.SH;
		} 
		return StockType.SZ;
	}
	
}
